package drongo.desktop.utils
{
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	/**
	 * 文件相关的工具类 
	 * @author Gaara
	 */	
	public class UtilsFile
	{
	
		/**
		 * 浏览选择文件 
		 * @param title
		 * @param callBack
		 * @param initiator
		 */
		public static function browseForOpen(title:String, filters:Array, callBack:Function, initiator:File = null) : void
		{
			initiator ||= new File;
			
			initiator.browseForOpen(title,filters);
			initiator.addEventListener(Event.SELECT,function(evt:Event):void
			{
				callBack(evt.target as File);
			});
		}
	
		/**
		 * 浏览保存文件 
		 * @param title
		 * @param callBack
		 * @param initiator
		 */
		public static function browseForSave(title:String,callBack:Function, initiator:File = null) : void
		{
			initiator ||= new File;
			
			initiator.browseForSave(title);
			initiator.addEventListener(Event.SELECT,function(evt:Event):void
			{
				callBack(evt.target as File);
			});
		}
		
		/**
		 * 浏览选择目录  
		 * @param title
		 * @param callBack
		 * @param initiator
		 */
		public static function browseForDirectory(title:String,callBack:Function,initiator:File = null):void
		{
			initiator ||= new File;
			
			initiator.browseForDirectory(title);
			initiator.addEventListener(Event.SELECT,function(evt:Event):void
			{
				callBack(evt.target as File);
			});
		}
		
		/**
		 * 保存二进制文件对象 
		 * @param file
		 * @param bytes
		 */
		public static function saveBytes(file:File,bytes:ByteArray):void
		{
			var fs:FileStream = new FileStream;
			fs.open(file,FileMode.WRITE);
			bytes.position = 0;
			fs.writeBytes(bytes);
			fs.close();
		}
		
		/**
		 * 保存JSON数据到本地
		 */
		public static function saveJson(file:File,json:String):void{
			var byte:ByteArray=new ByteArray();
			byte.writeUTFBytes(json);
			saveBytes(file,byte);
		}
		
		/**
		 * 加载xml 
		 * @param file
		 * @return 
		 */
		public static function loadXML(file:File) : XML
		{
			var xmlStr:String = loadString(file);
			if(xmlStr)
				return new XML(xmlStr);
			
			return null;
		}
		
		/**
		 * 保存xml 
		 * @param file
		 * @param xml
		 */
		public static function saveXML(file:File,xml:XML):void
		{
			saveString(file,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + xml.toXMLString());
		}
		
		/**
		 * 保存字符串 
		 * @param file
		 * @param context
		 * @param charSet
		 */
		private static function saveString(file:File, context:String,charSet:String = null):void
		{
			var bytes:ByteArray = new ByteArray;
			if(!charSet || charSet.toUpperCase() == "UTF-8")
				bytes.writeUTFBytes(context);
			else 
				bytes.writeMultiByte(context,charSet);
			
			saveBytes(file,bytes);
		}
		
		/**
		 * 读取字节码 
		 * @param file
		 * @param size
		 * @return 
		 */
		public static function loadBytes(file:File, size:int = int.MAX_VALUE):ByteArray
		{
			if(!file.exists)
				return null;
			
			var fs:FileStream = new FileStream;
			fs.open(file,FileMode.READ);
			var bytes:ByteArray = new ByteArray;
			fs.readBytes(bytes,0,Math.min(file.size,size));
			fs.close();
			
			return bytes;
		}
		
		/**
		 * 读取文件字符串 
		 * @param file
		 * @param charSet
		 * @param size
		 */
		public static function loadString(file:File,charSet:String = null,size:int = int.MAX_VALUE):String
		{
			var bytes:ByteArray = loadBytes(file,size);
			if(!bytes)
				return null;
			
			if(!charSet)
			{
				if(bytes.bytesAvailable > 2)
				{
					var firstValue:uint = bytes.readUnsignedByte();
					var secondValue:uint = bytes.readUnsignedByte();
					if(firstValue == 239 && secondValue == 187)
					{
						charSet = "utf-8";
						bytes.position++;
					}
					else if(firstValue == 254 && secondValue == 255)
					{
						charSet = "unicodeFFFE";
					}
					else if(firstValue == 255 && secondValue == 254)
					{
						charSet = "unicode";
					}
					else
					{
						charSet = "utf-8";
						bytes.position = bytes.position - 2;
					}
				}
				else
				{
					charSet = "utf-8";
				}
			}
			
			if(charSet.toLowerCase() == "utf-8")
				return bytes.readUTFBytes(bytes.length - bytes.position);
			
			return bytes.readMultiByte(bytes.length - bytes.position,charSet);
		}
		
		public static function deleteFile(file:File, moveToTrash:Boolean = false) : Boolean
		{
			try
			{
				if(moveToTrash)
				{
					file.moveToTrash();
				}
				else
				{
					file.deleteFile();
				}
				return true;
			}
			catch(e:Error)
			{
				if(e.errorID == 3001 && !moveToTrash)
				{
					try
					{
						file.moveToTrash();
					}
					catch(e:Error)
					{
					}
				}
				else if(e.errorID != 3003)
				{
				}
			}
			return false;
		}
		
		public static function copyFile(srcFile:File, dstFile:File) : Boolean
		{
			if(srcFile.nativePath == dstFile.nativePath)
			{
				return true;
			}
			deleteFile(dstFile);
			try
			{
				if(srcFile.exists)
				{
					srcFile.copyTo(dstFile,true);
					return true;
				}
				return false;
			}
			catch(e:Error)
			{
			}
			return false;
		}
	}
}